package com.company;

import java.io.*;

public class Task4 {


    public static void main(String[] args) {
       //todo 4.编写一个文件复制函数，copyFile(String from, String to)，from为原文件名，to为复制后的新文件名，复制结束打印所复制的文件大小和用时（毫秒），执行结果如：copy xxx use xxx ms, file size is xxx.
        String from_path = "D:/study/io/test.txt";
        String to_path = "D:/study/io/test2.txt";
        copyFile(from_path,to_path);
    }


    public static void copyFile(String from, String to) {
        BufferedReader reader = null;
        long startTime = System.currentTimeMillis();
        try {
            reader = new BufferedReader(new FileReader(from));
            File file = new File(to);
            file.delete();
            String line = null;
            while ((line = reader.readLine()) != null) {
                Writer output = new FileWriter(to, true);
                System.out.println(line);
                output.write(line + "\r\n");
                output.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try{
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                File file = new File(to);
                long endTime = System.currentTimeMillis();
                long diffTime = endTime - startTime;
                System.out.println("copy \"" + from + "\" use " + diffTime + "ms," + "file size is " + file.length() + "B");
            }
        }
    }

}
